package com.cj.wtrails.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.cj.wtrails.R;


/**
 * 自定义的一个dialog
 *
 * @author zengyi
 * create at 2019/11/26 9:14
 */

public final class AppDialog extends Dialog {

    private ConstraintLayout customLayout;// 自定义区块
    private TextView contentTv;
    private TextView descTv;
    private TextView negativeTv;
    private View divider;
    private TextView positiveTv;

    public AppDialog(Context context) {
        super(context, R.style.AppDialogStyle);
        initLayout(context);
    }

    private void initLayout(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.layout_app_dialog, null);
        customLayout = root.findViewById(R.id.customCl);
        contentTv = root.findViewById(R.id.contentTv);
        descTv = root.findViewById(R.id.descTv);
        negativeTv = root.findViewById(R.id.negativeTv);
        divider = root.findViewById(R.id.divider);
        positiveTv = root.findViewById(R.id.positiveTv);

        setContentView(root);

        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            int width = ScreenUtils.getScreenWidth(context);
            lp.width = (int) (width * 0.67);
        }
    }

    public static class Builder {
        private Context context;

        private String content;
        private int contentSize;
        private String contentColor;

        private String desc;
        private String descColor;

        private View customView;

        private String negativeText;
        private String negativeColor;
        private View.OnClickListener negativeListener;

        private String positiveText;
        private String positiveColor;
        private View.OnClickListener positiveListener;

        private boolean isCancelable;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setContentSize(String content, int size) {
            this.content = content;
            this.contentSize = size;
            return this;
        }

        public Builder setContent(String content, String color) {
            this.content = content;
            this.contentColor = color;
            return this;
        }

        public Builder setDesc(String desc) {
            return setDesc(desc, null);
        }

        public Builder setDesc(String desc, String color) {
            this.desc = desc;
            this.descColor = color;
            return this;
        }

        public void setCustomView(View view) {
            this.customView = view;
        }

        public Builder setNegativeButton(String text, View.OnClickListener listener) {
            return setNegativeButton(text, null, listener);
        }

        public Builder setNegativeButton(String text, String color, View.OnClickListener listener) {
            this.negativeText = text;
            this.negativeColor = color;
            this.negativeListener = listener;
            return this;
        }

        public Builder setPositiveButton(String text, View.OnClickListener listener) {
            return setPositiveButton(text, null, listener);
        }

        public Builder setPositiveButton(String text, String color, View.OnClickListener listener) {
            this.positiveText = text;
            this.positiveColor = color;
            this.positiveListener = listener;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            this.isCancelable = cancelable;
            return this;
        }

        public AppDialog build() {
            final AppDialog dialog = new AppDialog(context);

            if (customView != null) {
                // 自定义内容
                ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(-2, -2);
                lp.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
                lp.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
                lp.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
                lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
                customView.setLayoutParams(lp);

                dialog.customLayout.removeAllViews();
                dialog.customLayout.addView(customView);
            } else {
                // content
                dialog.contentTv.setText(content);
                if (!TextUtils.isEmpty(contentColor)) {
                    dialog.contentTv.setTextColor(Color.parseColor(contentColor));
                }

                if (contentSize != 0) {
                    dialog.contentTv.setTextSize(contentSize);
                }

                // desc
                if (!TextUtils.isEmpty(desc)) {
                    dialog.descTv.setText(desc);
                    dialog.descTv.setVisibility(View.VISIBLE);
                }
                if (!TextUtils.isEmpty(descColor)) {
                    dialog.descTv.setTextColor(Color.parseColor(descColor));
                }
            }

            // negative button
            int count = 0;
            if (!TextUtils.isEmpty(negativeText)) {
                count++;
                dialog.negativeTv.setText(negativeText);
                if (!TextUtils.isEmpty(negativeColor)) {
                    dialog.negativeTv.setTextColor(Color.parseColor(negativeColor));
                }
                dialog.negativeTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (negativeListener != null) {
                            negativeListener.onClick(v);
                        }
                        dialog.dismiss();
                    }
                });
                dialog.negativeTv.setVisibility(View.VISIBLE);
            }
            // positive button
            if (!TextUtils.isEmpty(positiveText)) {
                count += 2;
                dialog.positiveTv.setText(positiveText);
                if (!TextUtils.isEmpty(positiveColor)) {
                    dialog.positiveTv.setTextColor(Color.parseColor(positiveColor));
                }
                dialog.positiveTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (positiveListener != null) {
                            positiveListener.onClick(v);
                        }
                        dialog.dismiss();
                    }
                });
                dialog.positiveTv.setVisibility(View.VISIBLE);
            }

            if (count == 3) {// show all button
                dialog.divider.setVisibility(View.VISIBLE);
            }

            dialog.setCancelable(isCancelable);
            return dialog;
        }

        public void show() {
            build().show();
        }
    }
}
